package com.thinkit.bigdata.web.service.impl.bus;

import com.thinkit.bigdata.web.dao.bus.BusModelTypeMapper;
import com.thinkit.bigdata.web.model.bus.BusModelType;
import com.thinkit.bigdata.web.model.bus.BusModelTypeExample;
import com.thinkit.bigdata.web.service.bus.BusModelService;
import com.thinkit.bigdata.web.service.bus.BusModelTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class BusModelTypeServiceImpl implements BusModelTypeService {

    @Resource
    private BusModelTypeMapper busModelTypeMapper;

    @Resource
    private BusModelService busModelService;

    @Override
    public BusModelType selectByPrimaryKey(Long id) {
        return busModelTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BusModelType> selectByExample(BusModelTypeExample example) {
        return busModelTypeMapper.selectByExample(example);
    }

    @Override
    public int insert(BusModelType record) {
        return busModelTypeMapper.insertSelective(record);
    }

    @Override
    public int insertSelective(BusModelType record) {
        return busModelTypeMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(BusModelType record) {
        return busModelTypeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByExampleSelective(BusModelType record, BusModelTypeExample example) {
        return busModelTypeMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(BusModelType record, BusModelTypeExample example) {
        return busModelTypeMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKey(BusModelType record) {
        return busModelTypeMapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByExample(BusModelTypeExample example) {
        return busModelTypeMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id, Long userid) {
        int i = treeDel(id, userid);
        //删除分类,删除模型
        busModelTypeMapper.deleteByPrimaryKey(id);
        busModelService.deleteByTypeId(id);
        return i+1;
    }
    //递归删除子节点,同时删除模型
    private int treeDel(Long id, Long userid) {
        int i = 0;
        BusModelTypeExample example = new BusModelTypeExample();
        example.createCriteria()
                .andParentidEqualTo(id);
        List<BusModelType> busModelTypeList = busModelTypeMapper.selectByExample(example);
        //删除模型分类,同时删除模型,以及模型规则
        for (BusModelType modelType : busModelTypeList) {
            busModelTypeMapper.deleteByPrimaryKey(modelType.getId());
            busModelService.deleteByTypeId(modelType.getId());
            i++;
            deleteByPrimaryKey(modelType.getId(), userid);
        }
        return i;
    }

}
